
package VLSys;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Component;

import VLSObjects.*;

public class ManagementServiceAdd extends JPanel{
    
    
    String blank = "";
    String string[] = new String[5];
    
    JTextField serviceName;
    IntegerField serviceAmount;
    JComboBox serviceForSize;
    JComboBox serviceAvailablity;
    String[] sizeChoices = { "", "Small", "Medium","Large"};
    String[] availablityChoices = { "", "Available", "Unavailable"};
    JButton submitButton;
    JButton cancelButton;
    JLabel serviceNameLabel;
    JLabel serviceSizeLabel;
    JLabel serviceAmountLabel;
    JLabel serviceAvailabilityLabel;
    JLabel editServiceLabel;
    /*
    ImageIcon red = new ImageIcon("images/prompt_ask.png");
    ImageIcon notice = new ImageIcon("images/prompt_notice.png");
    */
    ImageIcon notice_green = new ImageIcon("images/prompt_notice_green.png");
    ImageIcon ask_red = new ImageIcon("images/prompt_ask_red.png");
    ImageIcon notice_red = new ImageIcon("images/prompt_notice_red.png");
    
    public ManagementServiceAdd(final JFrame frame)
    {
        
        setVisible(false);
        setLayout(null);
        setBackground(null);
        
               
        serviceName = new JTextField();
        serviceForSize = new JComboBox(sizeChoices);
        serviceAmount = new IntegerField();
        serviceAvailablity = new JComboBox(availablityChoices);
        
        submitButton = new JButton();
        cancelButton = new JButton();
        serviceNameLabel = new JLabel("Service:");
        serviceSizeLabel = new JLabel("Size:");
        serviceAmountLabel = new JLabel("Amount:");
        serviceAvailabilityLabel = new JLabel("Available:");
        
        editServiceLabel = new JLabel("ADD SERVICE");
        
               
        editServiceLabel.setFont(new Font("sansserif", Font.PLAIN, 40));
        
                
        editServiceLabel.setBounds(10,10,300,30);
        
        serviceNameLabel.setBounds(73,50,150,20);
        serviceSizeLabel.setBounds(92,75,150,20);
        serviceAmountLabel.setBounds(72,100,150,20);
        serviceAvailabilityLabel.setBounds(65,125,150,20);
        
        serviceName.setBounds(130,50,200,20);
        serviceName.setBorder(null);
        serviceForSize.setBounds(130,75,200,20);
        serviceForSize.setBorder(null);
        serviceAmount.setBounds(130,100,200,20);
        serviceAmount.setBorder(null);
        serviceAvailablity.setBounds(130,125,200,20);
        serviceAvailablity.setBorder(null);
        
        submitButton.setText("Submit");
        submitButton.setBounds(175,150,75,20);
        cancelButton.setText("Clear");
        cancelButton.setBounds(255,150,75,20);
        
        add(serviceName);
        add(serviceForSize);
        add(serviceAmount);
        add(serviceAvailablity);
        add(submitButton);
        add(cancelButton);
        add(serviceNameLabel);
        add(serviceSizeLabel);
        add(serviceAmountLabel);
        add(serviceAvailabilityLabel);
        add(editServiceLabel);

        serviceName.addKeyListener(new KeyAdapter() {
        
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode()==39 || e.getKeyCode()==37)
            {      
            }
            else 
                serviceName.setText(capitalizeFirstLetters(serviceName.getText()));
          }

          public void keyTyped(KeyEvent e) {
          }

          public void keyPressed(KeyEvent e) {
          }
        }
        );
        
        submitButton.addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                               
                string[0] = serviceName.getText();
                string[1] = (String)serviceForSize.getSelectedItem();
                string[2] = serviceAmount.getText();
                string[3] = (String)serviceAvailablity.getSelectedItem();
                
                if(string[0].length() == 0 && string[1] == blank && string[2].length() == 0 && string[3] == blank)
                {
                    checkEmpty();
                    JOptionPane.showMessageDialog(frame,
                                        "Incomplete Input",
                                        "Vehicle Logging Service",
                                        JOptionPane.WARNING_MESSAGE,
                                        notice_red);
                    
                } 
                else if(string[0].length()== 0 || string[1] == blank || string[2].equals(blank) || string[3] == blank)
                {
                    checkEmpty();
                    
                    if(string[0].length() > 25)
                    {
                        checkEmpty();
                        JOptionPane.showMessageDialog(frame,
                                            "Invalid and Incomplete Input",
                                            "Vehicle Logging Service",
                                            JOptionPane.WARNING_MESSAGE,
                                            notice_red);
                    }    

                    else if(string[0].length() == 0 || string[1] == blank || string[2].equals(blank) || string[3] == blank )
                        JOptionPane.showMessageDialog(frame,
                                        "Incomplete Input",
                                        "Vehicle Logging Service",
                                        JOptionPane.WARNING_MESSAGE,
                                        notice_red);
                }   
                else if(string[0].length() > 25)
                {
                    checkEmpty();
                    JOptionPane.showMessageDialog(frame,
                                        "Invalid Input",
                                        "Vehicle Logging Service",
                                        JOptionPane.WARNING_MESSAGE,
                                        notice_red);
                }            
                else if(string[0].length() <= 25 && string[1] != blank && string[2] != blank)   
                {
                    int n = JOptionPane.showConfirmDialog(frame,
                                        "Proceed with current values?",
                                        "Vehicle Logging Service",
                                        JOptionPane.YES_NO_OPTION,
                                        JOptionPane.QUESTION_MESSAGE,
                                        ask_red);

                    if(n==0)
                    {
                        Service newService = new Service();
                        ServiceManager newSm = new ServiceManager();


                        newService.setServiceName(string[0]);
                        newService.setSize(string[1]);
                        newService.setAmount(Integer.parseInt(string[2]));
                        
                        if(string[3].equals("Available") == true)
                            newService.setAvailable("Yes");
                        if(string[3].equals("Unavailable") == true)
                            newService.setAvailable("No");
                        
                       boolean existing = newSm.checkService(string[0], string[1],"--");
                       //check if a similar record already exists
                           
                       if(existing == true) 
                           {    JOptionPane.showMessageDialog(frame,
                                            "Service record already exists!",
                                            "Vehicle Logging Service",
                                            JOptionPane.WARNING_MESSAGE, notice_red);
                                resetValues();
                           }
                           else
                           {   //add to database if no similar record is found
                      
                            boolean result = newSm.addService(newService);

                            if (result == true)
                                JOptionPane.showMessageDialog(frame,
                                                "Service Added!",
                                                "Vehicle Logging Service",
                                                JOptionPane.WARNING_MESSAGE,
                                                notice_green);
                                resetValues();
                           }
                    }
                       
                }
            }
        }
        );
        
        cancelButton.addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                int n = JOptionPane.showConfirmDialog(
                            frame,
                            "Are you sure you want to clear the values?",
                            "Vehicle Logging Service",
                            JOptionPane.YES_NO_OPTION,
                            JOptionPane.QUESTION_MESSAGE,
                            ask_red);
                if(n==0){ 
                    resetValues();              
                }       
            }
        }
        );
    }
    
    public void checkEmpty()
    {   
        String s = "";
       
        if(serviceName.getText().length() == 0)
            serviceName.setBackground(Color.YELLOW);
               
        else if(serviceName.getText().length() > 25)
            serviceName.setBackground(Color.RED);
        
        else if(serviceName.getText().length() <= 25)
            serviceName.setBackground(null);
        
        if((String)serviceForSize.getSelectedItem() == s)
            serviceForSize.setBackground(Color.YELLOW);
        
        if((String)serviceForSize.getSelectedItem() != s)
            serviceForSize.setBackground(null);
        
        if( serviceAmount.getText().length() == 0)
            serviceAmount.setBackground(Color.YELLOW);
        
        if( serviceAmount.getText().length() != 0)
            serviceAmount.setBackground(null);
        
        if((String)serviceAvailablity.getSelectedItem() == s)
            serviceAvailablity.setBackground(Color.YELLOW);
        
                
        if((String)serviceAvailablity.getSelectedItem() != s)
            serviceAvailablity.setBackground(null);
        
    }
    
    public void resetValues()
    {
                serviceName.setBackground(null);
                serviceForSize.setBackground(null);
                serviceAmount.setBackground(null);
                serviceAvailablity.setBackground(null);

                serviceName.setText("");
                serviceForSize.setSelectedItem("");
                serviceAmount.setText("");
                serviceAvailablity.setSelectedItem("");   
       
    }
    
        public static String capitalizeFirstLetters ( String s ) {

        for (int i = 0; i < s.length(); i++) {

            if (i == 0) {
                // Capitalize the first letter of the string.
                s = String.format( "%s%s",
                             Character.toUpperCase(s.charAt(0)),
                             s.substring(1) );
            }

            // Is this character a non-letter or non-digit?  If so
            // then this is probably a word boundary so let's capitalize
            // the next character in the sequence.
            if (!Character.isLetterOrDigit(s.charAt(i))) {
                if (i + 1 < s.length()) {
                    s = String.format( "%s%s%s",
                                 s.subSequence(0, i+1),
                                 Character.toUpperCase(s.charAt(i + 1)),
                                 s.substring(i+2) );
                }
            }
        }
        return s;
    }
    
}
